package ltd.dujiabao.capability.domain.capmng.sync;

import java.time.LocalDateTime;
import java.util.Map;

import ltd.dujiabao.capability.common.domain.AuditableEntity;

/**
 * @author : DuJiabao
 * @Project : ddd-capability
 * @Time : 2024/4/6 21:37
 * @Desc :
 */
public class Worker extends AuditableEntity {
    private final Predicate predicate;
    private final Puller puller;
    private final Transferor transferor;

    public Worker(LocalDateTime createdAt, Long createdBy, Predicate predicate, Puller puller, Transferor transferor) {
        super(createdAt, createdBy);
        this.predicate = predicate;
        this.puller = puller;
        this.transferor = transferor;
    }

    public boolean checkIfNeeded(SyncCapabilityContext context) {
        return predicate.checkIfNeeded(context);
    }

    public void syncCapability(SyncCapabilityContext context) {
        Object original = puller.pullCapability(context);
        Map<String, Object> modules = transferor.transfer(original);
        context.getCapability().addModules(modules);
    }
}
